﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Com.DAO;
using WF.Model;
using WF.BusinessObject;
using WF.Common;

namespace WF.ViewModels.Workflow.KQApply
{
    public class KQApplyVM : ViewModelBase, System.Web.UI.IDataItemContainer
    {
        SessionManager sessionManager = new SessionManager();

        public virtual List<KQApplyEntity> GetEmptyEntity()
        {
            return new List<KQApplyEntity>();
        }

        public virtual void AddKQApply(KQApplyEntity kqentity)
        {
            #region
            if (kqentity.ApplicantName == string.Empty)
            {
                throw new BusinessException("请填写 [申请人姓名] ");
            }
            if (kqentity.ApplyDateFrom == null)
            {
                throw new BusinessException("请选择[请假开始时间]");
            }
            if (kqentity.ApplyDateTo == null)
            {
                throw new BusinessException("请选择 [请假结束时间] ");
            }
            //if (kqentity.ApplyDays <= 0)
            //{
            //    throw new BusinessException("请正确填写 [假期日期]");
            //}
            //if (kqentity.ApplyHours <= 0)
            //{
            //    throw new BusinessException("请正确填写 [假期小时数] ");
            //}
            if (kqentity.ApplyReason == string.Empty)
            {
                throw new BusinessException("请填写 [请假原因]");
            }
            if (kqentity.ApplyRegisterDate == null)
            {
                throw new BusinessException("请填写 [申请日期]");
            }
            if (string.IsNullOrEmpty(kqentity.HolidayTypeCode))
            {
                throw new BusinessException("请选择 [申请假期类型]");
            }
            if (string.IsNullOrEmpty(kqentity.HoliDayTypeName))
            {
                throw new BusinessException("请选择 [申请假期类型]");
            }

            if (kqentity.DeptName == string.Empty)
            {
                throw new BusinessException("请选择 [所在部门]");
            }

            if (kqentity.HolidayTypeCode == "2")
            {
                if (kqentity.ProvePersonAccount == string.Empty)
                {
                    throw new BusinessException("请填写 [忘记打卡证明人]");
                }
                else
                {
                    AD.AD pobjUtility = new AD.AD();
                    string domainAccount = kqentity.ProvePersonAccount;
                    if (pobjUtility.CheckADUserExist("", domainAccount) == false)
                    {
                        throw new Exception("忘记打卡证明人的公司账号[" + domainAccount + "]无效");
                    }

                    if (domainAccount.Trim() == kqentity.ApplyAccount.Trim())
                    {
                        throw new Exception("忘记打卡证明人的公司账号不能与申请人相同！");
                    }
                }
            }

            #endregion

            kqentity.Folio = string.Format("{0}的{1}", kqentity.ApplicantName, kqentity.HoliDayTypeName);

            ////同意申请人格式
            //if (!kqentity.ApplyAccount.Contains("centaline\\")) 
            //{
            //    kqentity.ApplyAccount = "centaline\\" + kqentity.ApplyAccount;
            //}
            //启动事务
            using (TransactionScope t = new TransactionScope())
            {
                //记录至数据库
                WF.BusinessObject.KQApply bo = CreateKQApply();
                bo.Add(kqentity);


                //记录至K2数据库
                UserProfile profile = sessionManager.CurrentUser;

                WF.DAL.WFDBDataContext wfcontext = bo.DataContext;
                string bizDataID = kqentity.ID;
                string applyTypeName = WorkFlowHelper.WF_APPLY_TYPE_KQAPPLY;

                WF_ApplyInstanceEntity applyinstanceEntity = new WF_ApplyInstanceEntity();

                applyinstanceEntity = WorkFlowHelper.AddWorkFlowInstance(wfcontext, applyTypeName, bizDataID, profile);

                WorkFlowHelper.StartProcessInstance(wfcontext, kqentity, applyinstanceEntity, profile);
                t.Complete();
                
            }
        }
    }
}
